<template>
  <div class="app-container detail-container">
    <div class="detail-title">
      {{ textMap['detail'] }}
    </div>
    <el-form ref="form" :rules="rules" :model="form" :label-width="labelWidth" class="detail-form">
      <el-row :gutter="100">
        <el-col :span="10">
          <el-form-item label="原密码" prop="oldPassword">
            <el-input v-model.trim="form.oldPassword" type="password" placeholder="请输入原密码" />
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model.trim="form.newPassword" type="password" placeholder="请输入新密码" />
          </el-form-item>
          <el-form-item label="再次输入" prop="renewPassword">
            <el-input v-model.trim="form.renewPassword" type="password" placeholder="请再次输入新密码" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div :style="{'padding-left': labelWidth}" class="detail-buttons">
      <el-button v-waves :loading="saveLoading" type="primary" @click="edit()">更新</el-button>
      <el-button v-waves type="default" @click="$$closeTag()">关闭</el-button>
    </div>
  </div>
</template>

<script>
import CloseTagMixin from '@/mixins/closeTag'
import { updatePassword } from '@/api/systemManage/user'
import waves from '@/directive/waves' // Waves directive
import SparkMD5 from 'spark-md5'

export default {
  name: 'FirmManageItem',
  directives: { waves },
  components: {
  },
  mixins: [CloseTagMixin],
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      textMap: {
        detail: '修改密码'
      },
      labelWidth: '100px',
      saveLoading: false,
      rules: {
        oldPassword: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 18, message: '密码长度在 6 到 18 个字符', trigger: 'blur' }
        ],
        renewPassword: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      form: {}
    }
  },
  computed: {
  },
  created() {
    console.log(this.$route)
    this.$store.dispatch('setTagName', {
      path: this.$route.path,
      title: `修改密码`
    })
    // 初始化数据模板
    this.formReset()
  },
  methods: {
    formReset() {
      this.form = {
        id: this.$store.getters.userId,
        oldPassword: '',
        newPassword: '',
        renewPassword: ''
      }
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    edit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.saveLoading = true
          this.form.oldPassword = this.getPasswordMd5(this.form.oldPassword)
          this.form.newPassword = this.getPasswordMd5(this.form.newPassword)
          this.form.renewPassword = this.getPasswordMd5(this.form.renewPassword)
          updatePassword(this.form).then(res => {
            this.saveLoading = false
            if (res.code === 200) {
              this.$message.success('更新成功！下次登录生效')
            } else {
              this.$message.error(res.message)
            }
          }).catch(e => {
            this.this.saveLoading = false
            this.$message.error(this.$store.getters.errorMsg)
          })
        } else return false
      })
    },
    getPasswordMd5(password) {
      const spark = new SparkMD5()
      spark.append(password)
      return spark.end()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
